<!doctype html>
<html lang="zh-CN">

<head>
    </base>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <title>工厂展示</title>
    <style>
        html,
        body,
        #container {
            height: 100%;
            width: 100%;
        }

        .content-window-card {
            position: relative;
            box-shadow: none;
            bottom: 0;
            left: 0;
            width: auto;
            padding: 0;
        }

        .content-window-card p {
            height: 2rem;
        }

        .custom-info {
            border: solid 1px silver;
        }

        div.info-top {
            position: relative;
            background: none repeat scroll 0 0 #F9F9F9;
            border-bottom: 1px solid #CCC;
            border-radius: 5px 5px 0 0;
        }

        div.info-top div {
            display: inline-block;
            color: #333333;
            font-size: 14px;
            font-weight: bold;
            line-height: 31px;
            padding: 0 10px;
        }

        div.info-top img {
            position: absolute;
            top: 10px;
            right: 10px;
            transition-duration: 0.25s;
        }

        div.info-top img:hover {
            box-shadow: 0px 0px 5px #000;
        }

        div.info-middle {
            font-size: 12px;
            padding: 10px 6px;
            line-height: 20px;
        }

        div.info-bottom {
            height: 0px;
            width: 100%;
            clear: both;
            text-align: center;
        }

        div.info-bottom img {
            position: relative;
            z-index: 104;
        }

        span {
            margin-left: 5px;
            font-size: 11px;
        }

        .info-middle img {
            float: left;
            margin-right: 6px;
        }
    </style>
</head>

<body>
    <div id="container"></div>
    <div class="info">
        点击地图上的点标记，打开所添加的自定义信息窗体
    </div>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=46ef4db4e56aebd71ccea52ad8802773">
    </script>
    <!-- UI组件库 1.0 -->
    <script src="//webapi.amap.com/ui/1.1/main.js?v=1.1.1"></script>
    <script type="text/javascript">
        // 创建地图
        var map = new AMap.Map('container', {
            resizeEnable: true,
            zoom: 4
        });
        //加载PointSimplifier，loadUI的路径参数为模块名中 'ui/' 之后的部分 
        AMapUI.loadUI(['misc/PointSimplifier'], function (PointSimplifier) {

            if (!PointSimplifier.supportCanvas) {
                alert('当前环境不支持 Canvas！');
                return;
            }

            //启动页面
            initPage(PointSimplifier);
        });

        function initPage(PointSimplifier) {
            //创建组件实例
            var pointSimplifierIns = new PointSimplifier({
                map: map, //关联的map
                compareDataItem: function (a, b, aIndex, bIndex) {
                    //数据源中靠后的元素优先，index大的排到前面去
                    return aIndex > bIndex ? -1 : 1;
                },
                getPosition: function (dataItem) {
                    //返回数据项的经纬度，AMap.LngLat实例或者经纬度数组
                    return dataItem.position;
                },
                getHoverTitle: function (dataItem, idx) {
                    //返回数据项的Title信息，鼠标hover时显示
                    return '序号: ' + idx;
                    // 根据id然后请求对应的数据就可以显示在卡片上
                },
                renderOptions: {
                    //点的样式
                    pointStyle: {
                        fillStyle: 'blue' //蓝色填充
                    }
                }
            });

            //随机创建一批点，仅作示意
            var data = local;
            // createPoints(map.getCenter(), 100000);
            //设置数据源，data需要是一个数组
            pointSimplifierIns.setData(data);


            //监听事件
            // pointMouseover pointMouseout
            pointSimplifierIns.on('pointClick ', function (e, record) {
                // 触发窗体信息显示详细信息 并且显示不同的位置
                //实例化窗口
                var title =
                    `${record.data.position[3]}<span style="font-size:11px;color:#F00;">价格:${record.data.position[2]}</span>`,
                    content = [];
                content.push(
                    `<img style="width:150px;height:100px" src=${record.data.position[6]}>地址：北京市朝阳区阜通东大街6号院3号楼东北8.3公里`);
                content.push(`电话：${record.data.position[7]}`);
                content.push("<a href='https://ditu.amap.com/detail/B000A8URXB?citycode=110105'>详细信息</a>");
                var infoWindow = new AMap.InfoWindow({
                    isCustom: true, //使用自定义窗体
                    content: createInfoWindow(title, content.join("<br/>")),
                    offset: new AMap.Pixel(16, -45)
                });
                infoWindow.open(map, record.data.position);
            });
        }

        //仅作示意坐标
        // function createPoints(center, num) {
        //     var data = [];
        //     for (var i = 0, len = num; i < len; i++) {
        //         data.push({
        //             position: [
        //                 // longitude 经度 latitude 纬度
        //                 center.getLng() + (Math.random() > 0.5 ? 1 : -1) * Math.random(),
        //                 center.getLat() + (Math.random() > 0.5 ? 1 : -1) * Math.random(),
        //             ]
        //         });
        //     }
        //     return data;
        // }


        //构建自定义信息窗体
        function createInfoWindow(title, content) {
            var info = document.createElement("div");
            info.className = "custom-info input-card content-window-card";

            //可以通过下面的方式修改自定义窗体的宽高
            //info.style.width = "400px";
            // 定义顶部标题
            var top = document.createElement("div");
            var titleD = document.createElement("div");
            var closeX = document.createElement("img");
            top.className = "info-top";
            titleD.innerHTML = title;
            closeX.src = "https://webapi.amap.com/images/close2.gif";
            closeX.onclick = closeInfoWindow;

            top.appendChild(titleD);
            top.appendChild(closeX);
            info.appendChild(top);

            // 定义中部内容
            var middle = document.createElement("div");
            middle.className = "info-middle";
            middle.style.backgroundColor = 'white';
            middle.innerHTML = content;
            info.appendChild(middle);

            // 定义底部内容
            var bottom = document.createElement("div");
            bottom.className = "info-bottom";
            bottom.style.position = 'relative';
            bottom.style.top = '0px';
            bottom.style.margin = '0 auto';
            var sharp = document.createElement("img");
            sharp.src = "https://webapi.amap.com/images/sharp.png";
            bottom.appendChild(sharp);
            info.appendChild(bottom);
            return info;
        }

        //关闭信息窗体
        function closeInfoWindow() {
            map.clearInfoWindow();
        }



        /* 封装ajax函数
         * @param {string}opt.type http连接的方式，包括POST和GET两种方式
         * @param {string}opt.url 发送请求的url
         * @param {boolean}opt.async 是否为异步请求，true为异步的，false为同步的
         * @param {object}opt.data 发送的参数，格式为对象类型
         * @param {function}opt.success ajax发送并接收成功调用的回调函数
         */
        function ajax(opt) {
            opt = opt || {};
            opt.method = opt.method.toUpperCase() || 'POST';
            opt.url = opt.url || '';
            opt.async = opt.async || true;
            opt.data = opt.data || null;
            opt.success = opt.success || function () {};
            var xmlHttp = null;
            if (XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            } else {
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            }
            var params = [];
            for (var key in opt.data) {
                params.push(key + '=' + opt.data[key]);
            }
            var postData = params.join('&');
            if (opt.method.toUpperCase() === 'POST') {
                xmlHttp.open(opt.method, opt.url, opt.async);
                xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
                xmlHttp.send(postData);
            } else if (opt.method.toUpperCase() === 'GET') {
                let token = sessionStorage.getItem('token');
                xmlHttp.open(opt.method, opt.url + postData, opt.async);
                xmlHttp.setRequestHeader('Authorization', token);
                xmlHttp.send(null);
            }
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    opt.success(xmlHttp.responseText);
                }
            };
        }
        var local;
        ajax({
            method: 'GET',
            url: 'http://localhost:8001/AgriculturalBigData/map/getPlantations',
            success: function (response) {
                local = JSON.parse(response).rst;
            }
        });
    </script>
</body>

</html>